package com.wiwf.model.security.config;

import com.wiwf.model.security.service.impl.CustomUserDetailsServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.annotation.Resource;

/**
 * Security 配置
 * @author kehui
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

	@Resource
	private PasswordEncoder passwordEncoder;

	@Resource
	private CustomUserDetailsServiceImpl userDatailService;

	@Value("${base.security.pages}")
	public String[] baseSecurityPages;

	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		auth.userDetailsService(userDatailService).passwordEncoder(passwordEncoder);
	}

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		// 放行接口
		http.authorizeHttpRequests()
				.antMatchers(baseSecurityPages)
				.permitAll().anyRequest().authenticated();
		// 定义验证
		http.httpBasic();
		// 禁用 frame 拦截
		http.headers().frameOptions().disable();
		// 禁用 csrf
		http.csrf().disable();
	}

}